import * as goodsDtailsApi from '@/api/goods';
import { getGoodsSkuDetail } from '../../api/goods.js';
export default {
  namespaced: true,
  state() {
    return {
      // 商品详情
      goodsDetails: {
        brand: [],
        categories: [],
        mainPictures: [],
        details: [],
        hotByDay: [],
        mainVideos: [],
        similarProducts: [],
        skus: [],
        specs: [],
        userAddresses: [],
      },
      // 商品同类推荐
      goodsRelevant: [],
      // 24小时热榜
      hotRank: [],
      // 周榜
      weekRank: [],
      // 商品sku详情
      goodsSkuDetail: [],
    };
  },
  getters: {
    // 将goodsRelevant分成四个一组
    goodsRelevantGroup(state) {
      const goodsRelevantGroup = [];
      for (let i = 0; i < state.goodsRelevant.length; i += 4) {
        goodsRelevantGroup.push(state.goodsRelevant.slice(i, i + 4));
      }
      return goodsRelevantGroup;
    },
  },
  mutations: {
    // 商品详情
    SET_GOODS_DETAILS(state, newValue) {
      state.goodsDetails = newValue;
    },
    // 商品SKU详情
    SET_GOODS_SKU_DETAIL(state, newValue) {
      state.goodsSkuDetail = newValue;
    },
    // 商品同类推荐
    SET_GOODS_RELEVANT(state, newValue) {
      state.goodsRelevant = newValue;
    },
    // 24小时热榜
    SET_HOT_RANK(state, newValue) {
      state.hotRank = newValue;
    },
    // 周榜
    SET_WEEK_RANK(state, newValue) {
      state.weekRank = newValue;
    },

  },
  actions: {
    // 商品详情
    async updateGoodsDetails({ commit }, id) {
      const res = await goodsDtailsApi.getGoodsDetail(id);
      commit('SET_GOODS_DETAILS', res);
    },
    // 商品SKU详情
    async updateGoodsSkuDetail({ commit }, id) {
      const res = await goodsDtailsApi.getGoodsSkuDetail(id);
      commit('SET_GOODS_SKU_DETAIL', res);
    },
    // 商品同类推荐
    async updateGoodsRelevant({ commit }, id) {
      const res = await goodsDtailsApi.getGoodsRelevant(id);
      commit('SET_GOODS_RELEVANT', res);
    },
    // 24小时热榜
    async updateHotRank({ commit }, id) {
      const res = await goodsDtailsApi.getHotRank(id);
      commit('SET_HOT_RANK', res);
    },
    // 周榜
    async updateWeekRank({ commit }, id) {
      const res = await goodsDtailsApi.getWeekRank(id);
      commit('SET_WEEK_RANK', res);
    },
  },
};
